import pinia from "./store";
import useUserStore from "./store/modules/user";
import router from "./router";
const useStore = useUserStore(pinia)

router.beforeEach((to,from,next)=>{
    const user = useStore.user;
    console.log('user',user);
    
    if(user && user._id){
        if(to.path == "/login"){
            next({path:"/"});
        }else{
            console.log(user.role?.menus);
            
            if(user.username != "admin"){
                if(user.role?.menus?.indexOf(to.path) != -1){
                    next()
                }else{
                    next({path:from.path})
                }
            }else{
                next()
            }
        }
    }else{
        if(to.path != "/login"){
            next({path:"/login"})
        }else{
            next()
        }
    }
})
export default router;